package com.example.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.Book;
import com.example.springboot.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public void insert(Book book){
        bookMapper.insert(book);
    }

    public Book select(LambdaQueryWrapper<Book> wrapper){
        Book book = bookMapper.selectOne(wrapper);
        return book;
    }

    public void update(Book book){
        bookMapper.updateById(book);
    }

    public void delete(Integer bookId){
        bookMapper.deleteById(bookId);
    }

    public Page<Book> select(Integer pageNum, Integer pageSize, String search){
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Book::getName,search);
        }
        Page<Book> bookPage = bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return bookPage;
    }

}
